<?php
namespace app\cdyoa\controller;
use think\Db;
class Kemu extends Base
{   
    public function lists(){
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            $sql = "
                SELECT
                    [1]
                FROM
                   kemu
                WHERE 
                    1 = ?
                ";
            $param = [1];
            if ( !empty($keyword) ) {
                $sql .= " and title like ?";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);
            
            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by id desc limit $start, $page_size";
            $field       = "*";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                //操作按钮
                $list[$k]['action'] = com_page(['ico'=>'edit','title'=>'编辑科目','url'=>url('kemu/edit','id='.$v['id']),'width'=>400,'height'=>200]);
            }
            $com_body    = com_build_table($list, array("id","title","create_time","action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
        $data = [
            'nav'       => ['报销科目', '列表'],
        ];
        return view('', $data);
    }

    //添加
    public function add(){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'title' =>  'require',
            ];
            $msg = [
                'title.require' => '标题必填',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['action_user_id']   = $this->userinfo['id'];
            $data['create_time'] = time();
            Db::name('kemu')->insert($data);
            $config = [
                'msg' => '添加成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        return view();
    }
    
    //编辑
    public function edit($id=''){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'title' =>  'require',
            ];
            $msg = [
                'title.require' => '标题必填',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['action_user_id']   = $this->userinfo['id'];
            $data['update_time'] = time();
            $data['id'] = $id;
            Db::name('kemu')->update($data);
            $config = [
                'msg' => '添加成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $d = [
            'info'      => Db::name('kemu')->find($id),
            'id'        => $id,
        ];
        return view('',$d);
    }
}
